<?php
//Overwritten functions for fpdf

require_once('fpdf.php');

class PDF extends FPDF
{
    
    var $height = 45;
    var $col    = 65;
    var $i      = 0;
    
    var $xpos;
    var $ypos;

    var $text_title = "";
    var $text_subtitle = "";
    
    var $date = 0;
    
    function Footer(){
        $this->SetTextColor(0,0,0);
        // Go to 1.5 cm from bottom
        $this->SetY(-15);
        // Select Arial italic 8
        $this->SetFont('Arial','I',8);
        // Print centered page number
        $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
    }

    function Header(){
        $this->Image(base_url().'img/rmplanner.png',null,null,null, 15);
        $this->SetDrawColor(20,50,150);
        $this->Line(10,27, 87, 27);
        $this->SetY(30);
    }

    public function setDrawColPos(){
        $iMod = $this->i % ($this->height * 3);
        if ($iMod == 0){      
            $this->AddPage();
            
            $this->PrintTitle();

            $this->SetFont('Helvetica','',10);
            $this->xpos = $this->getX();
            $this->ypos = $this->getY();
            $this->SetDrawColor(150,180,200);
            $this->Line($this->col + $this->lMargin - 3, $this->ypos, $this->col + $this->lMargin - 3, $this->h - $this->bMargin);
            $this->Line($this->col*2 + $this->lMargin - 3, $this->ypos, $this->col*2 + $this->lMargin - 3, $this->h - $this->bMargin);
        }

        if ($this->i % $this->height == 0 ){
            $this->setY($this->ypos);
        }
        if ($iMod < $this->height){
            //first row
            $this->setX($this->xpos);
        }
        else if ($iMod < $this->height*2){
            $this->setX($this->xpos + $this->col);
        }
        else{
            $this->setX($this->xpos + ($this->col*2));
        }
        $this->i++;
    }


    public function setTextTitle($title = "", $subtitle = ""){
        $this->text_title = $title;
        $this->text_subtitle = $subtitle;
    }
    
    public function printTitle(){
        $this->SetTextColor(0,0,0);

        $this->SetFont('Helvetica','B',20);
        $this->Cell(0,12, $this->text_title, 0 ,2);

        $this->SetTextColor(100,100,100);
        $this->SetFont('Helvetica','',12);
        $this->Cell(0,8, $this->text_subtitle, 0 ,2);
    }
    
    public function drawMealCell($meal){
        
        if ($meal['date'] != $this->date){
            $this->date = $meal['date'];
            
            $this->setY($this->GetY()+2);
            
            $this->SetFont('Helvetica','B',12);
            $this->SetTextColor(0,0,0);
            $this->Cell(0 ,6, "Meals for ".date("l jS \of F Y", $meal['date']), 0 ,1);
            $this->SetDrawColor(100, 150, 200);
            $this->Line(10, $this->GetY()+0.5 ,20 + $this->GetStringWidth("Meals for ".date("l jS \of F Y", $meal['date'])), $this->GetY() + 0.5 );
            $this->setY($this->GetY()+3);
        }
        
        $this->SetTextColor(0,0,0);
        $this->SetFont('Helvetica','B',10);
        $this->Cell($this->GetStringWidth($meal['title'].": "),5, $meal['title'].": ", 0 ,0);
        
        $this->SetTextColor(100,100,100);
        $this->SetFont('Helvetica','',8);
        $this->Cell($this->getStringWidth("Averages "),5, "Averages ",0,0); 
        
        $this->SetTextColor(0,0,0);
        $this->SetFont('Helvetica','B',8);
        
        $this->Cell($this->getStringWidth(round($meal['ratio'], 3)." votes"), 5, round($meal['ratio'], 3)." votes", 0, 0);
        
        $this->SetTextColor(100,100,100);
        $this->SetFont('Helvetica','',8);
        $this->Cell($this->GetStringWidth(" from a total of "),5, " from a total of ", 0, 0);
        
        $this->SetFont('Helvetica', 'B', 8);
        $this->SetTextColor(0, 0, 0);
        $this->cell($this->GetStringWidth($meal['mealtime_count']), 5, $meal['mealtime_count'], 0,0);
        
        $this->SetFont('Helvetica', '', 8);
        $this->SetTextColor(100, 100, 100);
        $this->cell(0, 5, " servings", 0,1);
        
        $this->SetFont('Helvetica','B',7);
        $this->SetTextColor(0,0,0);
        $this->Cell($this->GetStringWidth("Ingredients: "),5, "Ingredients: ", 0 ,0);
        $this->SetTextColor(100,100,100);
        $this->SetFont('Helvetica','',7);
        $this->MultiCell(100,5, $meal['ingredients'], 0 ,1);
        $this->Cell(0,2, "", 0 ,1);
        
        
    }

// End of class
}